<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增定额工日单价')" />
    <th:block th:include="include :: datetimepicker-css" />
    <style type="text/css">
        table{
            border: 1px solid;
            width: 100%;
        }

        table td,th{
            border: 0.0625rem solid;
            text-align: center;
        }
        .number-text{
            width:100%;
            text-align:center;
            border:none;
        }
    </style>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-price-add">
            <div class="row">
                <div class="col-sm-12">
                    <div class="ibox">
                        <div class="ibox-title">
                            <h5>基础信息</h5>
                        </div>
                        <div class="ibox-content">
                            <div class="form-group">
                                <label class="col-sm-3 control-label is-required">单价日期：</label>
                                <div class="col-sm-8">
                                    <div class="input-group date">
                                        <input required name="unitPriceDate" id="unitPriceDate" class="form-control" placeholder="yyyy-MM" type="text" readonly>
                                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-3 control-label">备注：</label>
                                <div class="col-sm-8">
                                    <textarea name="remark" id="remark" class="form-control"></textarea>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12">
                    <div class="ibox">
                        <div class="ibox-title">
                            <h5>定额工日单价信息</h5>
                        </div>
                        <div class="ibox-content">
                            <div class="form-group">
                                <div class="col-sm-12">
                                    <div class="container-div" >
                                        <table id="priceTb">
                                            <tr>
                                                <th>种类</th>
                                                <th colspan="2">工程类别</th>
                                                <th>最大单价（元/工日）</th>
                                                <th>最小单价（元/工日）</th>
                                                <th>平均单价（元/工日）</th>
                                            </tr>

                                            <tr th:each="cate : ${categorys}">
                                                <th rowspan="16">[[${cate.name}]]</th>
                                                <td colspan="2">[[${cate.children[0].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[0].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[0].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[0].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[0].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[0].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[0].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <th rowspan="2">[[${cate.children[1].name}]]</th>
                                                <td >[[${cate.children[1].children[0].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[1].children[0].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[1].children[0].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[1].children[0].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[1].children[0].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[1].children[0].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[1].children[0].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td>[[${cate.children[1].children[1].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[1].children[1].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[1].children[1].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[1].children[1].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[1].children[1].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[1].children[1].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[1].children[1].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td rowspan="2">[[${cate.children[2].name}]]</td>
                                                <td>[[${cate.children[2].children[0].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[2].children[0].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[2].children[0].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[2].children[0].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[2].children[0].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[2].children[0].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[2].children[0].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td>[[${cate.children[2].children[1].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[2].children[1].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[2].children[1].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[2].children[1].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[2].children[1].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[2].children[1].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[2].children[1].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td rowspan="5">[[${cate.children[3].name}]]</td>
                                                <td>[[${cate.children[3].children[0].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[3].children[0].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[3].children[0].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[3].children[0].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[3].children[0].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[3].children[0].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[3].children[0].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td>[[${cate.children[3].children[1].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[3].children[1].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[3].children[1].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[3].children[1].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[3].children[1].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[3].children[1].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[3].children[1].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td>[[${cate.children[3].children[2].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[3].children[2].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[3].children[2].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[3].children[2].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[3].children[2].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[3].children[2].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[3].children[2].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td>[[${cate.children[3].children[3].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[3].children[3].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[3].children[3].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[3].children[3].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[3].children[3].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[3].children[3].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[3].children[3].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td>[[${cate.children[3].children[4].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[3].children[4].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[3].children[4].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[3].children[4].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[3].children[4].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[3].children[4].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[3].children[4].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td rowspan="2">[[${cate.children[4].name}]]</td>
                                                <td>[[${cate.children[4].children[0].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[4].children[0].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[4].children[0].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[4].children[0].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[4].children[0].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[4].children[0].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[4].children[0].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td>[[${cate.children[4].children[1].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[4].children[1].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[4].children[1].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[4].children[1].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[4].children[1].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[4].children[1].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[4].children[1].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td rowspan="4">[[${cate.children[5].name}]]</td>
                                                <td>[[${cate.children[5].children[0].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[5].children[0].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[5].children[0].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[5].children[0].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[5].children[0].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[5].children[0].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[5].children[0].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td>[[${cate.children[5].children[1].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[5].children[1].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[5].children[1].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[5].children[1].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[5].children[1].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[5].children[1].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[5].children[1].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td>[[${cate.children[5].children[2].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[5].children[2].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[5].children[2].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[5].children[2].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[5].children[2].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[5].children[2].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[5].children[2].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                            <tr th:each="cate : ${categorys}">
                                                <td>[[${cate.children[5].children[3].name}]]</td>
                                                <input type="hidden" name="categoryId" th:value="${cate.children[5].children[3].id}"  />
                                                <input type="hidden" name="unitPriceId" th:value="${cate.children[5].children[3].unitPriceId}"  />
                                                <input type="hidden" name="id" th:value="${cate.children[5].children[3].detailId}"  />
                                                <td><input type="number"  name="minPrice" th:value="${cate.children[5].children[3].minPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number"  name="maxPrice" th:value="${cate.children[5].children[3].maxPrice}" class="number-text" onblur="cal(this)"/></td>
                                                <td><input type="number" name="avgPrice" th:value="${cate.children[5].children[3].avgPrice}" class="number-text" readonly/></td>
                                            </tr>
                                        </table>
                                    </div>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "basic/price"
        var datas = [[${@dict.getType('sys_normal_disable')}]];
        $("#form-price-add").validate({
            onkeyup: false,
            rules:{
                unitPriceDate:{
                    remote: {
                        url: prefix + "/checkUnitPriceDateUnique?type=1",
                        type: "post",
                        dataType: "json",
                        data: {
                            "unitPriceDate": function() {
                                return $("input[name='unitPriceDate']").val();
                            }
                        },
                        dataFilter: function(data, type) {
                            return $.validate.unique(data);
                        }
                    }
                }
            },
            messages: {
                "unitPriceDate": {
                    remote: "该月份定额工日单价已经存在"
                }
            },
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var param =  {};
                param["detailList"] = getTableData();
                param["remark"] = $("#remark").val();
                param["unitPriceDate"] = $("#unitPriceDate").val();
                param["type"] = 1;
                $.operate.saveJson(prefix + "/add", JSON.stringify(param));
            }
        }

        $("input[name='unitPriceDate']").datetimepicker({
            format: "yyyy-mm",
            startView:'year',
            maxView:'year',
            minView:'year',
            autoclose: true
        });
        $("input[name='unitPriceDate']").change(function(){
            $("input[name='unitPriceDate']").focus();
        });
        function getTableData(){
            var data = [];
            $('#priceTb tr').each(function(i){  // 遍历 tr
                var el = {};
                $(this).children('td').each(function(j){  // 遍历 tr 的各个 td
                    if($(this).find("input[name='minPrice']").length > 0){
                        el["minPrice"] = $(this).find("input[name='minPrice']").val();
                    }
                    if($(this).find("input[name='maxPrice']").length > 0){
                        el["maxPrice"] = $(this).find("input[name='maxPrice']").val();
                    }
                    if($(this).find("input[name='avgPrice']").length > 0){
                        el["avgPrice"] = $(this).find("input[name='avgPrice']").val();
                    }
                });
                if($(this).find("input").length > 0){
                    el["categoryId"] = $(this).find("input").val();
                    data.push(el);
                }
            });
            return data;
        }

        function cal(tis){
            var tr = $(tis).parent().parent();
            var minPrice = Number($(tr).find("input[name='minPrice']").val());
            var maxPrice = Number($(tr).find("input[name='maxPrice']").val());
            $(tr).find("input[name='avgPrice']").val(Number((maxPrice+minPrice)/2).toFixed(2));
        }
    </script>
</body>
</html>